package cn.fl.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import cmm.comm.facade.framework.condition.SysRegionQueryCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysRegionApiService;
import cmm.comm.facade.framework.dto.SysRegionDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.service.ISysRegionService;
import cn.fl.system.transform.SysRegionVOTransform;
import cn.fl.system.vo.SysRegionVO;

/**
 * @Description: 地区表 客户端服务层接口实现
 * @Description: 持久层方法名称前缀：新增：insert* 删除：delete* 修改：update* 批量：batch* 查询对象：select* get* find* 查询集合：select*
 */
@Service
public class SysRegionServiceImpl implements ISysRegionService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(ISysRegionService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private ISysRegionApiService sysRegionApiService;

	@Override
	public SysRegionVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return null;
		//TODO 业务逻辑
		SysRegionDTO dto = null;//this.自定义ApiService.selectById(id);
		return SysRegionVOTransform.toVO(dto);
	}

	@Override
	public Long add(SysRegionVO entity) throws BaseException{
		if(null == entity)return null;
		//return this.sysRegionApiService.add(SysRegionVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return false;
		//return this.sysRegionApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(SysRegionVO entity) throws BaseException{
		if(null == entity)return false;
		//return this.sysRegionApiService.modify(SysRegionVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		 Pager pager = null;//this.sysRegionApiService.searchListPage(page);
		 if(CollectionUtils.isNotEmpty(page.getResultList())){
			 List<SysRegionDTO> resultList = (List<SysRegionDTO>) page.getResultList();
			 pager.setResultList(SysRegionVOTransform.toVOList(resultList));//dto 转  vo
		 }
		return pager;
	}

	@Override
	public List<SysRegionVO> searchProvinceList() throws BaseException {
		String sysCd = CurrentThreadContext.getCurrentSysCd();
		List<SysRegionDTO> list = this.sysRegionApiService.searchProvinceList(sysCd);
		return SysRegionVOTransform.toVOList(list);
	}

	@Override
	public List<SysRegionVO> searchRegineByRegCd(String rgnCd)
			throws BaseException {
		if(StringUtils.isEmpty(rgnCd)){
			BaseLogUtils.info(logger, "searchRegineByRegCd", "根据区域代码查询子级失败,区域代码参数为空.rgnCd:"+rgnCd,CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		List<SysRegionDTO> list = new ArrayList<SysRegionDTO>();
		String sysCd = CurrentThreadContext.getCurrentSysCd();
		SysRegionDTO sysRegionDTO = this.sysRegionApiService.getRegionByRgnCd(rgnCd, sysCd);
		if(null!=sysRegionDTO){
			list = this.sysRegionApiService.searchRegineByRgnPid(sysRegionDTO.getId(), sysCd);
		}
		return SysRegionVOTransform.toVOList(list);
	}

	@Override
	public List<KeyValueVO> searchProvinceKVList() throws BaseException {
		List<KeyValueVO> kvList = new ArrayList<KeyValueVO>();
		List<SysRegionVO> list = this.searchProvinceList();
		if(CollectionUtils.isNotEmpty(list)){
			for (SysRegionVO sysRegionVO : list) {
				KeyValueVO kv = new KeyValueVO();
				kv.setKey(sysRegionVO.getRgnCd());
				kv.setValue(sysRegionVO.getRgnNm());
				kvList.add(kv);
			}
		}
		return kvList;
	}

	@Override
	public List<KeyValueVO> searchRegineKVList(String rgnCd)
			throws BaseException {
		List<KeyValueVO> kvList = new ArrayList<KeyValueVO>();
		List<SysRegionVO> list = this.searchRegineByRegCd(rgnCd);
		if(CollectionUtils.isNotEmpty(list)){
			for (SysRegionVO sysRegionVO : list) {
				KeyValueVO kv = new KeyValueVO();
				kv.setKey(sysRegionVO.getRgnCd());
				kv.setValue(sysRegionVO.getRgnNm());
				kvList.add(kv);
			}
		}
		return kvList;
	}


	/**
	 * 根据区域名称模糊查询所有关集合
	 * @return
	 * @throws BaseException
	 */
	@Override
	public List<SysRegionDTO> searchOrgRegRListByRegNm(SysRegionQueryCondition condition, String sysCd) throws BaseException {
		if(null == condition){
			condition = new SysRegionQueryCondition();
			condition.setRgnLvlMax(2);
		}else{
			if(StringUtils.isEmpty(condition.getRgnLvlMax())){
				condition.setRgnLvlMax(2);
			}
		}
		if(StringUtils.isEmpty(condition.getRecRgnLvlMin())){
			condition.setRecRgnLvlMin(1);
		}
		if(StringUtils.isEmpty(condition.getRecRgnLvlMax())){
			condition.setRecRgnLvlMax(2);
		}
		return this.sysRegionApiService.searchOrgRegRListByRegNm(condition,sysCd);
	}


	@Override
	public List<SysRegionDTO> searchSysRegionByPid(Long rgnPid, String sysCd) throws BaseException {
		SysRegionQueryCondition qc = new SysRegionQueryCondition();
		qc.setRgnPid(rgnPid);
		qc.setSysCd(sysCd);
		qc.setRgnLvlMax(2);
		return this.sysRegionApiService.searchRegionList(qc);//.searchRegineByRgnPid(rgnPid,sysCd);
	}

	@Override
	public SysRegionVO getRegionByRgnCd(String rgnCd)throws BaseException {
		if(StringUtils.isEmpty(rgnCd)){
			BaseLogUtils.info(logger, "searchRegineByRegCd", "根据区域代码查询区域对象,区域代码参数为空.rgnCd:"+rgnCd,CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		String sysCd = CurrentThreadContext.getFlServiceSysCd();
		SysRegionDTO sysRegionDTO = this.sysRegionApiService.getRegionByRgnCd(rgnCd, sysCd);
		return   SysRegionVOTransform.toVO(sysRegionDTO);
	}


}
